package spring.cloud.service.task.controller;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spring.cloud.service.task.dto.NamespaceDto;
import spring.cloud.service.task.service.ZooKeeperService;
import spring.cloud.service.task.utils.PageInfo;
import spring.cloud.service.task.utils.Result;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("/namespace")
@RestController
public class NameSpaceController {

    @Autowired
    private ZooKeeperService zooKeeperService;

    /**
     * page: 1
     * limit: 10
     */
    @GetMapping(value = "/getNamespaceList")
    public Result getNamespace(@RequestParam(required = false) String zookeeperKey,//
                               @RequestParam(required = false) String description,//
                               @RequestParam(required = false) String status, //
                               @RequestParam(required = false) Integer page, //
                               @RequestParam(required = false) Integer limit) throws RuntimeException {
        Result result = new Result();
        result.setCode(Result.table_query_success_code);
        result.setMsg(Result.table_query_success_msg);
        status = StringUtils.trimToNull(status);//下拉框""为空BUG fix
        PageInfo pageInfo = zooKeeperService.getNamespaceList(zookeeperKey, description, status, page, limit);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", pageInfo.getTotalCount());
        map.put("records", pageInfo.getRecords());
        result.setData(map);
        return result;
    }

    @PostMapping(value = "/addNamespace")
    public Result addNamespace(@RequestBody String data)
            throws RuntimeException {
        try {
            String userId = "CXL";
            NamespaceDto namespaceDto = new NamespaceDto();
            namespaceDto.setCreatedBy(userId);
            namespaceDto.setCreateTime(new Date());
            namespaceDto.setLastUpdatedBy(userId);
            namespaceDto.setLastUpdateTime(new Date());
            //
            JSONObject jsonObject = JSONObject.parseObject(data);
            namespaceDto.setDescription((String) jsonObject.get("description"));
            namespaceDto.setZookeeperKey((String) jsonObject.get("zookeeperKey"));
            namespaceDto.setNamespaceKey((String) jsonObject.get("namespaceKey"));//必须全局唯一
            namespaceDto.setStatus((String) jsonObject.get("status"));
            namespaceDto.setHotPeriodStartDate((String) jsonObject.get("hotPeriodStartDate"));
            namespaceDto.setHotPeriodEndDate((String) jsonObject.get("hotPeriodEndDate"));
            namespaceDto.setTaskMaxNum(jsonObject.getInteger("taskMaxNum"));
            Result result = zooKeeperService.addNamespace(namespaceDto, userId);
            return result;
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            Result result = Result.fail("添加命名空间失败");
            return result;
        }
    }

    @PostMapping(value = "/updateNamespace")
    public Result updateNamespace(@RequestBody String data)
            throws RuntimeException {
        try {
            String userId = "CXL";
            NamespaceDto namespaceDto = new NamespaceDto();
            //nameSpaceDto.setCreatedBy(userId);
            //nameSpaceDto.setCreateTime(new Date());
            namespaceDto.setLastUpdatedBy(userId);
            namespaceDto.setLastUpdateTime(new Date());
            //
            JSONObject jsonObject = JSONObject.parseObject(data);
            namespaceDto.setDescription((String) jsonObject.get("description"));
            namespaceDto.setZookeeperKey((String) jsonObject.get("zookeeperKey"));
            namespaceDto.setNamespaceKey((String) jsonObject.get("namespaceKey"));//必须全局唯一
            namespaceDto.setStatus((String) jsonObject.get("status"));
            namespaceDto.setHotPeriodStartDate((String) jsonObject.get("hotPeriodStartDate"));
            namespaceDto.setHotPeriodEndDate((String) jsonObject.get("hotPeriodEndDate"));
            namespaceDto.setTaskMaxNum(jsonObject.getInteger("taskMaxNum"));
            Result result = zooKeeperService.updateNamespace(namespaceDto, userId);
            return result;
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            Result result = Result.fail("修改命名空间失败");
            return result;
        }
    }

    @PostMapping(value = "/updateNamespaceStatus")
    public Result updateNamespaceStatus(@RequestBody String data) throws RuntimeException {
        com.alibaba.fastjson2.JSONObject jsonObject = JSON.parseObject(data);
        String namespaceKey = jsonObject.getString("namespaceKey");
        String status = jsonObject.getString("status");
        Result result = zooKeeperService.updateNamespaceStatus(namespaceKey, status);
        return result;
    }

    @PostMapping(value = "/deleteNamespace")
    public Result deleteNamespace(@RequestParam("namespaceKey") String namespaceKey) throws RuntimeException {
        Result result = zooKeeperService.deleteNamespace(namespaceKey);
        return result;
    }

    //
    @GetMapping(value = "/getNamespaceKeyAndDescriptionList")
    public Result getNamespace() throws RuntimeException {
        Result result = zooKeeperService.getNamespaceKeyAndDescriptionList();
        return result;
    }
}
